package com.hui.redis.controller;

import com.hui.redis.redisUtil.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: liuhui
 * @createDate: 2020/3/20
 * @version: 1.0
 */
@RestController
@Api(value = "测试")
public class redisDemo {
    @Autowired
    private RedisUtils redisUtils;
    @PostMapping("/setToken")
    @ApiOperation(value = "设置token")
    public String setToken(String token){
        /*设置生成有效期*/
        long time = 60;
        boolean falge = redisUtils.set("token",time, token);
        return falge ? "redis测试成功":"redis测试失败";
    }
    @PostMapping("/getToken")
    @ApiOperation(value = "获取token")
    public String getToken(String token){
        Long expire = redisUtils.getExpire(token);  /*获取有效时间*/
        return redisUtils.get(token).toString()+"剩余有效时间为："+expire.toString();
    }

    @PostMapping("/updateToken")
    @ApiOperation(value = "更新token")
    public String updateToken(String token,String value){
        if(redisUtils.exist(token)){
            Long expire1 = redisUtils.getExpire(token);
            if(expire1 >= 10){
                return "token不允许更新";
            }
            String newToken = "newToken";
            redisUtils.set(token,30L,value);    /*设置旧token的过期时间*/
            redisUtils.set(newToken,120L,value);   /*设置新token的过期时间*/
            return newToken;    /*将新的token返回给前台，前台将新的token进行放入请求同头中*/
        }
        Long expire = redisUtils.getExpire(token);  /*获取有效时间*/
        return redisUtils.get(token).toString()+"剩余有效时间为："+expire.toString();
    }

}
